//
//  PingLunViewController.m
//  Experss
//
//  Created by wmwMAC on 17/8/10.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "PingLunViewController.h"

#import "ButtonAndLabels.h"//头部

#import "PingLunTableViewCell.h"
#import "PingLunCollectionTableView.h"


#import "HYBStarEvaluationView.h"
#import "CommentListModel.h"


static NSString *CollectionIdentifier = @"PingLunCollectionTableView";
static NSString *identifier = @"PingLunTableViewCell";
@interface PingLunViewController ()<UITableViewDelegate,UITableViewDataSource,ButtonAndLabelsDelegate>
{
    NSInteger btnSelectTag;
    NSMutableArray *btnArr;
    
    int _page;
    NSString *changTitlStr;//全部， 好评，中评。差评
    UIView *titleView;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)NSMutableArray *dataArray22;
@property (nonatomic, strong)NSMutableArray *numArray2;

@end

@implementation PingLunViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"评论";
    _dataArray = [NSMutableArray array];
    _dataArray22 = [NSMutableArray array];
    _numArray2  = [NSMutableArray array];
    
    _page = 1;
    changTitlStr= @"0";
    btnSelectTag = 3000;
    [self addTableviewUI];
    [self getCommrntDataFromApi];//获取评论列表数据
}



- (void)addTableviewUI {
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, kScreenWidth, kScreenHeight-64-44-20) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = BaseColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"PingLunTableViewCell" bundle:nil] forCellReuseIdentifier:identifier];
    [self.tableView registerClass:[PingLunCollectionTableView class] forCellReuseIdentifier:CollectionIdentifier];
    [self.view addSubview:self.tableView];
    
   
    
    
    
    MJRefreshGifHeader *headNew = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestRefresh)];
    NSMutableArray *refreshingImages2 = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [refreshingImages2 addObject:image];
    }
    
    NSMutableArray *idleImages2 = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [idleImages2 addObject:image];
    }
    // 设置普通状态的动画图片
    [headNew setImages:idleImages2 forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [headNew setImages:refreshingImages2 forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [headNew setImages:refreshingImages2 duration:2 forState:MJRefreshStateRefreshing];
    // 隐藏状态
    headNew.stateLabel.hidden = YES;
    headNew.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = headNew;
    
    
    
    
    
    MJRefreshBackGifFooter *_header = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestDtaLoadMore)];
    NSMutableArray *refreshingImages = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [refreshingImages addObject:image];
    }
    
    NSMutableArray *idleImages = [NSMutableArray array];
    for (NSUInteger i =1; i<=12; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld",i]];
        [idleImages addObject:image];
    }
    // 设置普通状态的动画图片
    [_header setImages:idleImages forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [_header setImages:refreshingImages forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [_header setImages:refreshingImages duration:2 forState:MJRefreshStateRefreshing];
    // 隐藏状态
    _header.stateLabel.hidden = YES;
    self.tableView.mj_footer = _header;
    
    
}

#pragma mark －＝－＝－＝－＝－－＝－＝＝＝－＝评论刷新与加载
- (void)requestRefresh {
    _page = 1;
    [self getCommrntDataFromApi];
}
- (void)requestDtaLoadMore {
    _page++;
    [self getCommrntDataFromApi];
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        self.tableView.rowHeight = UITableViewAutomaticDimension;
        self.tableView.estimatedRowHeight = 240;
        return self.tableView.rowHeight;
    }else{
        return ((kScreen_Width - 50 - 60 )/3.0+20)*1;
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CommentListModel *model = self.dataArray[section];
    
    if (model.images.count == 0) {
        return 1;
    }else {
        return 2;
    }
   
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CommentListModel *model = self.dataArray[indexPath.section];
    
    if (indexPath.row == 0) {
        PingLunTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        HYBStarEvaluationView *starViewUnClick = [[HYBStarEvaluationView alloc]initWithFrame:CGRectMake(0, 0, 100, 15) numberOfStars:5 isVariable:NO];
        starViewUnClick.actualScore =[model.level intValue];
        starViewUnClick.fullScore = 5;
        starViewUnClick.isContrainsHalfStar = NO;
        [cell.levelView addSubview:starViewUnClick];
        
        
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.headimgurl]placeholderImage:[UIImage imageNamed:@"quetu"]];
        
        cell.nameLB.text = model.nickname;
        
        cell.timeLB.text = model.addtime;
        cell.commentLB.text = model.desc;

        return cell;

    }else{
    
        PingLunCollectionTableView *cell =  [tableView dequeueReusableCellWithIdentifier:CollectionIdentifier forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dataArray = model.images;
        return cell;
    
    }
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    if (section == 0) {
//        
//        return 60;
//    }else {
        return 0.01;
//    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = BaseColor;

    
    
    
    return headerView;
}

#pragma mark －＝－＝－－＝不同的评价切换点击事件－＝＝＝＝＝＝
-(void)buttonViewbtnClick:(NSInteger)index{
    
    
    for (NSInteger i = 3000; i<3005; i++) {
        ButtonAndLabels *buttonViews = [self.view viewWithTag:i];
        buttonViews.SecondLable.textColor = [UIColor blackColor];
        buttonViews.FirstLabel.textColor = [UIColor blackColor];
    }
    
    ButtonAndLabels *buttonViews = [self.view viewWithTag:index];
    buttonViews.SecondLable.textColor = BaseBlueColor;
    buttonViews.FirstLabel.textColor = BaseBlueColor;
    
    NSArray *aray = @[@"0",@"1",@"2",@"3"];
    changTitlStr = aray[index-3000];
    _page =1;
    //获取评论列表
    [self getCommrntDataFromApi];
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footview = [[UIView alloc] init];
    footview.backgroundColor = BaseColor;
    
    return footview;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark －－－－－－－－－＝＝＝＝＝＝＝＝获取评论列表数据
- (void)getCommrntDataFromApi {
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"userid":saveDic[@"userid"],@"goods_id":@"13"
                                ,@"evaluate":changTitlStr,
                                @"page":[NSString stringWithFormat:@"%d",_page],
                                @"limit":@"20",
                                @"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    
    [HttpRequest getWithURLString:POST(Shop_GoodsDetailGetGoodsComment) parameters:parameDic success:^(id responseObject) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (_page == 1) {
            [_dataArray removeAllObjects];
        }
        if ([responseObject[@"ret"] intValue] != 200) {
            [MBProgressHUD showText:@"获取商品评论出错"];
            return ;
        }
        
        
        
        NSDictionary *tempD = responseObject[@"data"];
        
        
        
        
        if (![[NSString stringWithFormat:@"%@",tempD[@"code"]] isEqualToString:@"0"]) {
            [MBProgressHUD showText:tempD[@"msg"]];
            return;
        }
        
        if (_numArray2 ) {
            [_numArray2 removeAllObjects];
        }
        //获取各种评论数量
        NSDictionary *tempDicNum = tempD[@"count"];
        [_numArray2 addObject:tempDicNum[@"evaluate_0"]];
        [_numArray2 addObject:tempDicNum[@"evaluate_1"]];
        [_numArray2 addObject:tempDicNum[@"evaluate_2"]];
        [_numArray2 addObject:tempDicNum[@"evaluate_3"]];
        
        
        [self configureTitleV];
        
        _dataArray22= [CommentListModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
        [_dataArray addObjectsFromArray:_dataArray22.mutableCopy];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
}



- (void)configureTitleV {
    if (titleView) {
        [titleView removeFromSuperview];
    }
    
    titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    
    NSArray *titleNameArr = @[@"全部",@"好评",@"中评",@"差评"];
    NSArray *numberArr = [NSArray arrayWithArray:_numArray2];
    CGFloat pice_weidth = kScreenWidth/4;
    for (NSInteger j=0; j<numberArr.count; j++) {
        
        ButtonAndLabels *buttonViews = [[ButtonAndLabels alloc] initWithFrame:CGRectMake(pice_weidth*j, 0, pice_weidth, 60)];
        buttonViews.delegate = self;
        buttonViews.tag = j+3000;
        buttonViews.FirstLabel.text = titleNameArr[j];
        buttonViews.SecondLable.text = numberArr[j];
        if (j == [changTitlStr intValue]) {
            buttonViews.SecondLable.textColor = BaseBlueColor;
            buttonViews.FirstLabel.textColor = BaseBlueColor;
        }
        
        [titleView addSubview:buttonViews];
        
        [btnArr addObject:buttonViews];
        
    }
    
    [self.view addSubview:titleView];;
}







@end
